<?php

function dolog($content){
    $date = date('Ymd');
    $fp = fopen('./log/'.$date.'.log','a+');
    $wdata = date('Y-m-d H:i:s')."：".$content."\r\n";
    fwrite($fp, $wdata);
    fclose($fp);
}


function do_jpush($uid,$alert,$exreas){
    require_once './jpush/autoload.php';
    $client = new \JPush\Client(DOMAIN_JPUSH_APP_KEY, DOMAIN_JPUSH_MASTER_SECRET);
    try {
        $is_all = 0;
        if(!is_array($uid)){
            $uid = array($uid);
        }
        foreach ($uid as $k=>$v){
            if($v == 0){
                $is_all = 1;
            }
            $uid[$k] = (string)$v;
        }
        $re = $client->push()->setPlatform('all');
        
        if($is_all == 1){
            $re->addAllAudience();
        }else{
            $re->addAlias($uid);
        }
        
        $re->options(array('apns_production'=>true));
        
        $re->addIosNotification($alert,'default',1,false,'',$exreas)
        ->addAndroidNotification($alert,NULL,0,$exreas)
        ->send();
        dolog('ok-'.json_encode($re));
        return 1;
    } catch (\JPush\Exceptions\APIConnectionException $e) {
        dolog('err1-'.$e->__toString());
        return -1;
    } catch (\JPush\Exceptions\APIRequestException $e) {
        dolog('err2-'.$e->__toString());
        return -2;
    }
}


function getUidToken($uid){
    $sign1 = descstr((string)$uid);
    $sign2 = "7{$sign1}68";
    $sign3 = (int)$sign2 + 24167896;
    $sign4 = $sign3.'al@.0806!#TM';
    $sign5 = md5($sign4);
    $sign6 = substr($sign5,8,16);
    return $sign6;
    /*
    $u1 = ($uid + 7597698) * 2;
    $re_num_arr = array();
    for ($ii = 0 ; $ii < strlen($u1) ; $ii++){
        $tmp_num = substr($u1, $ii , 1);
        if($ii == 1 || $ii == (strlen($u1)-1)){
            $re_num_arr[] = rand(0, 9);
        }
        $re_num_arr[] = $tmp_num;
    }
    $uidtoken = '';
    foreach ($re_num_arr as $v){
        $uidtoken .= (string)$v;
    }
    return $uidtoken;
    */
}


function descstr($str){
    $len=strlen($str); //函数返回字符串的长度：
    $newstr="";
    for($i=$len-1;$i>=0;$i--){
        $newstr.=$str[$i];
    }
    return $newstr;
}



